package com.TeacherInfoServer.mapper;

import com.TeacherInfoServer.pojo.DTO.DepartmentDTO;
import com.TeacherInfoServer.pojo.DTO.SearchDTO.DepartmentSearchDTO;
import com.TeacherInfoServer.pojo.entity.Department;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 教研室信息mapper接口
 */
public interface DepartmentMapper extends BaseMapper<Department> {
    @Select("SELECT d.id, d.name, l.teacher_name AS leaderName, r.teacher_name AS directorName " +
            "FROM Department d " +
            "LEFT JOIN Teacher l ON d.leader_id = l.teacher_id " +
            "LEFT JOIN Teacher r ON d.director_id = r.teacher_id " +
            "ORDER BY d.name "  // 假设您想按照部门名称排序
            )
    Page<DepartmentDTO> getDepartmentListByPage(Page<?> page, @Param("qw") QueryWrapper<Department> queryWrapper);
    @Select("SELECT d.id, d.name, l.teacher_name AS leaderName, r.teacher_name AS directorName " +
            "FROM Department d " +
            "LEFT JOIN Teacher l ON d.leader_id = l.teacher_id " +
            "LEFT JOIN Teacher r ON d.director_id = r.teacher_id"
    )
    List<DepartmentDTO> selectDepartmentWithLeadersAndDirectors();

    @Select("<script>" +
            "SELECT d.id, d.name, l.teacher_name AS leaderName, r.teacher_name AS directorName " +
            "FROM Department d " +
            "LEFT JOIN Teacher l ON d.leader_id = l.teacher_id " +
            "LEFT JOIN Teacher r ON d.director_id = r.teacher_id " +
            "<where> " +
            "<if test='params.name != null and params.name.trim() != \"\"'> " +
            "AND d.name LIKE CONCAT('%', #{params.name}, '%') " +
            "</if> " +
            "<if test='params.leaderName != null and params.leaderName.trim() != \"\"'> " +
            "AND l.teacher_name LIKE CONCAT('%', #{params.leaderName}, '%') " +
            "</if> " +
            "<if test='params.directorName != null and params.directorName.trim() != \"\"'> " +
            "AND r.teacher_name LIKE CONCAT('%', #{params.directorName}, '%') " +
            "</if> " +
            // 其他字段的模糊查询条件
            "</where>" +
            "</script>")
    Page<DepartmentDTO> searchDepartmentList(Page<?> page, @Param("params") DepartmentSearchDTO params);

}
